const http = require('http');

const app = http.createServer((req,res)=>{

    // 1. 获取请求的查询字符串信息
    // console.log(req.url;);

    res.setHeader('content-type','text/html;charset=utf-8');


    // 处理过后的url对象
    // 参数1：req.url
    // 参数2：当前主机地址
    let newUrl = new URL(req.url,'http://127.0.0.1');

    console.log(newUrl);

    // URL {
    //     href: 'http://127.0.0.1/admin?name=laowang&age=18',
    //     origin: 'http://127.0.0.1',
    //     protocol: 'http:',
    //     username: '',
    //     password: '',
    //     host: '127.0.0.1',
    //     hostname: '127.0.0.1',
    //     port: '',
    //     pathname: '/admin',
    //     search: '?name=laowang&age=18',
    //     searchParams: URLSearchParams { 'name' => 'laowang', 'age' => '18' },
    //     hash: ''
    //     host: '127.0.0.1',
    //     hostname: '127.0.0.1',
    //     port: '',
    //     pathname: '/favicon.ico',
    //     search: '',
    //     searchParams: URLSearchParams {},
    //     hash: ''
    //   }

    res.end(`${req.url}
        <br/>
        ${req.url.split('=')[1]}<br/>
        ${newUrl}<br/>
        ${newUrl.searchParams.get('name')}<br/>
        ${newUrl.searchParams.get('age')}<br />
        ${newUrl.pathname}
    `);
})

app.listen(80,()=>{
    console.log('启动成功');
});